using System;
using UnityEngine;

namespace ZyGame.Game
{
    public class PhysiceComponent : EntityComponent
    {
        private EntityPhysiceContext _physiceContext;
        private TransformComponent _transformComponent;

        public override void Awake(params object[] dataList)
        {
            base.Awake(dataList);
            _transformComponent = entity.GetComponent<TransformComponent>();
            _physiceContext = _transformComponent.gameObject.AddComponent<EntityPhysiceContext>();
            _physiceContext.entity = this.entity;
            _physiceContext.physice = this;
        }

        public virtual void OnEntryCollider(Entity entity)
        {
            Client.Console.WriteLine("[ENTRY]" + entity.guid);
        }

        public virtual void OnExitCollider(Entity entity)
        {
            Client.Console.WriteLine("[EXIT]" + entity.guid);
        }

        public virtual void OnStayCollider(Entity entity)
        {
            Client.Console.WriteLine("[STAY]" + entity.guid);
        }

        class EntityPhysiceContext : MonoBehaviour
        {
            public Entity entity;
            public PhysiceComponent physice;

            private void OnCollisionEnter(Collision other)
            {
                EntityPhysiceContext entityPhysiceContext = other.gameObject.GetComponent<EntityPhysiceContext>();
                if (entityPhysiceContext is null)
                {
                    return;
                }

                physice.OnEntryCollider(entityPhysiceContext.entity);
            }

            private void OnCollisionExit(Collision other)
            {
                EntityPhysiceContext entityPhysiceContext = other.gameObject.GetComponent<EntityPhysiceContext>();
                if (entityPhysiceContext is null)
                {
                    return;
                }

                physice.OnExitCollider(entityPhysiceContext.entity);
            }

            private void OnCollisionStay(Collision other)
            {
                EntityPhysiceContext entityPhysiceContext = other.gameObject.GetComponent<EntityPhysiceContext>();
                if (entityPhysiceContext is null)
                {
                    return;
                }

                physice.OnStayCollider(entityPhysiceContext.entity);
            }

            private void OnTriggerEnter(Collider other)
            {
                EntityPhysiceContext entityPhysiceContext = other.gameObject.GetComponent<EntityPhysiceContext>();
                if (entityPhysiceContext is null)
                {
                    return;
                }

                physice.OnEntryCollider(entityPhysiceContext.entity);
            }

            private void OnTriggerExit(Collider other)
            {
                EntityPhysiceContext entityPhysiceContext = other.gameObject.GetComponent<EntityPhysiceContext>();
                if (entityPhysiceContext is null)
                {
                    return;
                }

                physice.OnExitCollider(entityPhysiceContext.entity);
            }

            private void OnTriggerStay(Collider other)
            {
                EntityPhysiceContext entityPhysiceContext = other.gameObject.GetComponent<EntityPhysiceContext>();
                if (entityPhysiceContext is null)
                {
                    return;
                }

                physice.OnStayCollider(entityPhysiceContext.entity);
            }
        }
    }
}